package com.zky.bargain.price.TimeTask;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zky.bargain.price.dao.BargainDao;
import com.zky.bargain.price.dao.UserBargainDao;
import com.zky.bargain.price.entity.BargainEntity;
import com.zky.bargain.price.entity.UserBargainEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class TaskDemo {

    @Autowired
    private BargainDao bargainDao;

    @Autowired
    private UserBargainDao userBargainDao;

    @Scheduled(cron = "0/10 * * * * ? ")
    public void task(){

        List<UserBargainEntity> list = userBargainDao.selectList(new QueryWrapper<UserBargainEntity>().eq("status", 0));
        UserBargainEntity userBargainEntity = list.get(0);

        Long bargainId = userBargainEntity.getBargainId();
        BargainEntity bargainEntity = bargainDao.selectById(bargainId);

        Date createTime = userBargainEntity.getCreateTime();//发起砍价时间
        Date bargainEndTime = bargainEntity.getBargainEndTime();//砍价结束时间

        System.out.println("监听:未发现砍价活动结束的");
        if (bargainEndTime.getTime()<createTime.getTime()){
            userBargainEntity.setStatus("3");//砍价发起失效

            userBargainDao.updateById(userBargainEntity);

            bargainEntity.setBargainStatus("3");//朋友帮砍失败
            bargainDao.updateById(bargainEntity);
            System.out.println("监听:发现砍价活动超时的");
        }

//        for (UserBargainEntity userBargainEntity : list) {
//
//            Long bargainId = userBargainEntity.getBargainId();
//            BargainEntity bargainEntity = bargainDao.selectById(bargainId);
//
//            Date bargainEndTime = bargainEntity.getBargainEndTime();//结束时间
//            Date createTime = userBargainEntity.getCreateTime();//发起时间
//
//            if (bargainEndTime.getTime()<createTime.getTime()){
//
//                userBargainDao.updateById();
//            }
//        }

    }

}
